/*
 * servlet qui sert à montrer comment on charge les notes des utilisateurs
 */
package servlets;

import gestionnaires.*;
import helpers.AmazonHelper;
import helpers.YouTubeHelper;
import utilisateurs.modeles.Film;
import utilisateurs.modeles.Utilisateur;
import utilisateurs.modeles.Image;
import utilisateurs.modeles.Film;
import helpers.AmazonHelper;
import helpers.YouTubeHelper;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utilisateurs.modeles.*;


@WebServlet(name = "LoadServlet", urlPatterns = {"/LoadServlet"})
public class LoadServlet extends HttpServlet {

  ///////////////////////// GESTIONNAIRES //////////////////////////////////
    @EJB
    private GestionnaireDeFilms videoGameFacade;
    @EJB
    private GestionnaireDeCategorie categoryFacade;
    @EJB
    private GestionnaireTags tagFacade;
    @EJB
    private GestionnaireImage imageFacade;
    @EJB
    private GestionnaireGroupeImage imagesGroupFacade;
    @EJB
    private GestionnaireUtilisateurs userFacade;
    @EJB
    private GestionnairePanier cartFacade;
    @EJB
    private GestionnaireDeCategorieAge ageRatingFacade;
    @EJB
    private GestionnaireAdresse addressFacade;
   ////////////////////////////////////////////////////////////////////////////

    public GestionnaireImage getImageFacade() {
        return imageFacade;
    }

    public GestionnaireDeCategorie getCategoryFacade() {
        return categoryFacade;
    }

    public GestionnaireGroupeImage getImagesGroupFacade() {
        return imagesGroupFacade;
    }

    public GestionnaireDeFilms getVideoGameFacade() {
        return videoGameFacade;
    }

    public GestionnaireTags getTagFacade() {
        return tagFacade;
    }

    public GestionnaireDeCategorieAge getAgeRatingFacade() {
        return ageRatingFacade;
    }

    @Override
    public void init() {
        ageRatingFacade.createAll(imageFacade);
        AmazonHelper.getXML(this);
        YouTubeHelper.getXML(this);

        List<Utilisateur> users = userFacade.createAdmins(addressFacade, cartFacade);
        Collection<Film> videoGames = videoGameFacade.getAll();
        Map<Integer, String> notes = new HashMap<Integer, String>();
        notes.put(0, "Bullshit");
        notes.put(1, "Very poor");
        notes.put(2, "Not that bad");
        notes.put(3, "Average");
        notes.put(4, "Good");
        notes.put(5, "Perfect");
        Random random = new Random();

        // Générations de commentaires et de notes aléatoires
        for (Film videoGame : videoGames) {
            int totalVotes = random.nextInt(11);

            for (int i=0; i<totalVotes; i++) {
                Note note = new Note(random.nextInt(6));
                Commentaire comment = new Commentaire(users.get(random.nextInt(2)), notes.get(note.getNote()), note);
                videoGameFacade.addComment(comment, videoGame);
            }
        }
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String dir = request.getParameter("dir");
        String path = "SiteServlet";

        if (dir != null && dir.equals("admin")) {
            path = "admin/AdminServlet";
        }

        RequestDispatcher dispatcher = request.getRequestDispatcher(path);
        dispatcher.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}